package edu.hubu.talentmanagementsystem.gen;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;

/**
 * @author moonlan
 * date 2021/3/3 下午12:55
 */
@SuppressWarnings(value = "unused")
public class MapperGen {
    public static void main(String[] args) {
        String path = "/home/moonlan/code/java/talent-management-system/src/main/resources/ftls/";
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_29);
        //String genPath = "/home/moonlan/code/java/talent-management-system/src/main/java/edu/hubu" +
        //        "/talentmanagementsystem/mapper/";
        String genPath = "/home/moonlan/桌面/mapper/";
        File file = new File(genPath);
        if (!file.exists()) {
            file.mkdir();
        }
        try {
            configuration.setDirectoryForTemplateLoading(new File(path));
            List<Class<?>> classes = GenTool.getClasses("edu.hubu.talentmanagementsystem.model.ext");
            for (Class<?> clazz : classes) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("modelName", clazz.getSimpleName().replace(clazz.getSimpleName().charAt(0),
                        (char) (clazz.getSimpleName().charAt(0) + 32)));
                map.put("modelClassName", clazz.getSimpleName());
                Template template = configuration.getTemplate("mapper.ftl");
                FileWriter writer =
                        new FileWriter(genPath + clazz.getSimpleName
                                () + "Mapper.java");
                template.process(map, writer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
